//
//  LZCreatNewGroupOrderObjectView.m
//  laziz_Merchant
//
//  Created by xbnzd on 17/4/1.
//  Copyright © 2017年 XBN. All rights reserved.
//

#import "LZCreatNewGroupOrderObjectView.h"
#import "extobjc.h"
#import "LZRemindView.h"
#import "IQKeyboardManager.h"

@interface LZCreatNewGroupOrderObjectView()
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bgViewTopToSuperCST;
@property (weak, nonatomic) IBOutlet UIView *bgView;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UIView *nameBgView;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UITextField *nameTF;
@property (weak, nonatomic) IBOutlet UIView *priceBgView;
@property (weak, nonatomic) IBOutlet UILabel *priceLabel;
@property (weak, nonatomic) IBOutlet UITextField *priceTF;
@property (weak, nonatomic) IBOutlet UIButton *confirmBtn;

@end

@implementation LZCreatNewGroupOrderObjectView

- (void)awakeFromNib
{
    [super awakeFromNib];
    
    [self configureUI];
}

#pragma mark - UI
- (void)configureUI
{
    GET_SCREEN_SCALE(scale);
    self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.6];
    self.bgViewTopToSuperCST.constant = 200*scale;
    self.bgView.backgroundColor = [UIColor whiteColor];
    self.bgView.layer.cornerRadius = 6;
    self.bgView.layer.masksToBounds = YES;
    
    self.titleLabel.font = [UIFont systemFontOfSize:18*scale];
    self.titleLabel.text = LZLocalizedString(@"add_object_of_the_group", nil);
    
    self.nameBgView.layer.cornerRadius = 4;
    self.nameBgView.layer.masksToBounds = YES;
    self.nameBgView.layer.borderColor = UIColorFromRGB(0xcccccc, 1).CGColor;
    self.nameBgView.layer.borderWidth = 1;
    
    self.nameLabel.font = [UIFont systemFontOfSize:18*scale];
    self.nameLabel.text = LZLocalizedString(@"name", nil);
    self.nameTF.font = [UIFont systemFontOfSize:15*scale];
    self.nameTF.placeholder = LZLocalizedString(@"enter_name", nil);
    
    self.priceBgView.layer.cornerRadius = 4;
    self.priceBgView.layer.masksToBounds = YES;
    self.priceBgView.layer.borderColor = UIColorFromRGB(0xcccccc, 1).CGColor;
    self.priceBgView.layer.borderWidth = 1;
    
    self.priceLabel.font = [UIFont systemFontOfSize:18*scale];
    self.priceLabel.text = LZLocalizedString(@"original_price", nil);
    self.priceTF.font = [UIFont systemFontOfSize:15*scale];
    self.priceTF.placeholder = LZLocalizedString(@"enter_original_price", nil);
    
    self.confirmBtn.layer.cornerRadius = 20;
    self.confirmBtn.layer.masksToBounds = YES;
    [self.confirmBtn setTitle:LZLocalizedString(@"confirm", nil) forState:UIControlStateNormal];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(closeKeyboardAction)];
    [self addGestureRecognizer:tap];
    
    UITapGestureRecognizer *tapO = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(closeKeyboardAction)];
    [self.bgView addGestureRecognizer:tapO];
    
}

#pragma mark - method
- (void)showView
{
    GET_SCREEN_SCALE(scale);
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    
    self.hidden = NO;
    self.alpha = 0;
    @weakify(self);
    [UIView animateWithDuration:0.3 animations:^{
        @strongify(self);
        self.alpha = 1;
    } completion:^(BOOL finished) {
        @strongify(self);
        [self.nameTF becomeFirstResponder];
        if (SCREEN_WIDTH <= 320) {
            self.bgViewTopToSuperCST.constant = 90*scale;
            [UIView animateWithDuration:0.2 animations:^{
                [self layoutIfNeeded];
            }];
        }else if (SCREEN_WIDTH > 320 && SCREEN_WIDTH <= 375){
            self.bgViewTopToSuperCST.constant = 130*scale;
            [UIView animateWithDuration:0.2 animations:^{
                [self layoutIfNeeded];
            }];
        }else{
            self.bgViewTopToSuperCST.constant = 150*scale;
            [UIView animateWithDuration:0.2 animations:^{
                [self layoutIfNeeded];
            }];
        }
    }];
}

- (void)hiddenView
{
    @weakify(self);
    [UIView animateWithDuration:0.3 animations:^{
        @strongify(self);
        self.alpha = 0;
        
    } completion:^(BOOL finished) {
        
        [self removeFromSuperview];
    }];
}

- (void)closeKeyboardAction
{
    [self.nameTF resignFirstResponder];
    [self.priceTF resignFirstResponder];
}

#pragma mark - btn action
- (IBAction)closeBtnAction:(id)sender {
    
    [self hiddenView];
}
- (IBAction)confirmBtnAction:(id)sender {
    
    if (self.nameTF.text.length == 0) {
        [LZRemindView initWithImage:[UIImage imageNamed:@"错误提示"] andContent:LZLocalizedString(@"enter_name", @"")];
        return;
    }
    
    if (self.priceTF.text.length == 0) {
        [LZRemindView initWithImage:[UIImage imageNamed:@"错误提示"] andContent:LZLocalizedString(@"enter_original_price", @"")];
        return;
    }
    
    if ([self.priceTF.text floatValue] < 0 || [self.priceTF.text floatValue] > 100) {
        return;
    }

    if (self.addNewGroupOrderObjectBlock) {
        self.addNewGroupOrderObjectBlock(self.nameTF.text, self.priceTF.text);
        [self hiddenView];
    }
}


@end
